//
//  ViewController.swift
//  ActiveLabelDemo
//
//  Created by Johannes Schickling on 9/4/15.
//  Copyright © 2015 Optonaut. All rights reserved.
//

import UIKit
import ActiveLabel

class ViewController: UIViewController {
    
    let label = ActiveLabel()
    
    let label1 = ActiveLabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        let customType = ActiveType.custom(pattern: "\\sare\\b") //Looks for "are"
        let customType2 = ActiveType.custom(pattern: "\\sit\\b") //Looks for "it"
        let customType3 = ActiveType.custom(pattern: "\\ssupports\\b") //Looks for "supports"
        let customType4 = ActiveType.custom(pattern: "\\s[图片]\\b")
        let customType5 = ActiveType.custom(pattern: "\\[图片\\]")

        label.enabledTypes = [customType, customType2, customType3, customType4, customType5, ActiveType.unfold(pattern: " 展开")]

//        label.urlMaximumLength = 31

        label.customize { label in
            label.text = "This is a post with #multiple #hashtags and a @userhandle. Links are also supported like" +
            " this one: http://optonaut.co. Now it also supports custom patterns -> are\n\n" +
                "Let's trim a long link: \nhttps://twitter.com/twicket_app/status/649678392372121601"
            label.numberOfLines = 0
            label.lineSpacing = 4
            
            label.textColor = UIColor(red: 102.0/255, green: 117.0/255, blue: 127.0/255, alpha: 1)
            label.hashtagColor = UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)
            label.mentionColor = UIColor(red: 238.0/255, green: 85.0/255, blue: 96.0/255, alpha: 1)
            label.URLColor = UIColor(red: 85.0/255, green: 238.0/255, blue: 151.0/255, alpha: 1)
            label.URLSelectedColor = UIColor(red: 82.0/255, green: 190.0/255, blue: 41.0/255, alpha: 1)

            label.handleMentionTap { self.alert("Mention", message: $0) }
            label.handleHashtagTap { self.alert("Hashtag", message: $0) }
            label.handleUnfoldTap { self.alert("Unfold", message: $0) }
            label.handleURLTap { self.alert("URL", message: $0.absoluteString) }

            //Custom types
            label.customColor[customType] = UIColor.purple
            label.customSelectedColor[customType] = UIColor.green
            label.customColor[customType2] = UIColor.magenta
            label.customSelectedColor[customType2] = UIColor.green
            
//            label.configureLinkAttribute = { (type, attributes, isSelected) in
//                var atts = attributes
//                switch type {
//                case customType3:
//                    atts[NSAttributedString.Key.font] = isSelected ? UIFont.boldSystemFont(ofSize: 16) : UIFont.boldSystemFont(ofSize: 14)
//                default: ()
//                }
//                
//                return atts
//            }

            label.handleCustomTap(for: customType) { self.alert("Custom type", message: $0) }
            label.handleCustomTap(for: customType2) { self.alert("Custom type", message: $0) }
            label.handleCustomTap(for: customType3) { self.alert("Custom type", message: $0) }
        }

        label.frame = CGRect(x: 20, y: 140, width: view.frame.width - 40, height: 180)
        label.backgroundColor = UIColor.black
//        view.addSubview(label)
        
        
        
        
//        label1.enabledTypes.append(customType)
//        label1.enabledTypes.append(customType2)
//        label1.enabledTypes.append(customType3)
//        label1.enabledTypes.append(customType4)
//        label1.enabledTypes.append(customType5)
        
        label1.enabledTypes = [.url, .unfold(pattern: " 展开")]
        label1.backgroundColor = UIColor.black

//        label1.urlMaximumLength = 31

        label1.customize { label in
            
            let attributedString = NSMutableAttributedString.init()
            
            attributedString.append(NSAttributedString(string: "🎉🎉🎉点赞并评论本条活动，抽取30名幸运同学赠十二时辰主题街区门票！！！\n抽奖时间：2023年12月8日\n\n·11月9日—12月9日   在小红书或抖音平台发布你在长安十二时辰主题街区的唐装照片/视频\n·带话题#谁还不是唐潮人，同时@长安十二时辰主题街区即可参与最美唐潮人评选\nhttps://mp.weixin.qq.com/s/0r6wg4H4BGc5hntUSRgBxA", attributes: [.font: UIFont.boldSystemFont(ofSize: 14) as Any, .foregroundColor: UIColor.lightText]))
//            attributedString.append(NSAttributedString(string: "asdasdasd This is a post with #multiple #hashtags and a @userhandle. Links are also supported like this one. Now it also supports custom patterns -> are Let's trim a long link", attributes: [.font: UIFont.boldSystemFont(ofSize: 16) as Any, .foregroundColor: UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)]))
//            attributedString.append(NSAttributedString(string: "[图片]", attributes: [.font: UIFont.boldSystemFont(ofSize: 16) as Any, .foregroundColor: UIColor.black]))
            
            label.attributedText = attributedString
            label.numberOfLines = 0
            label.lineSpacing = 0
            
//            label.textColor = UIColor(red: 102.0/255, green: 117.0/255, blue: 127.0/255, alpha: 1)
//            label.hashtagColor = UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)
//            label.mentionColor = UIColor(red: 238.0/255, green: 85.0/255, blue: 96.0/255, alpha: 1)
//            label.URLColor = UIColor(red: 85.0/255, green: 238.0/255, blue: 151.0/255, alpha: 1)
//            label.URLSelectedColor = UIColor(red: 82.0/255, green: 190.0/255, blue: 41.0/255, alpha: 1)

//            label.handleMentionTap { self.alert("Mention", message: $0) }
//            label.handleHashtagTap { self.alert("Hashtag", message: $0) }
//            label.handleURLTap { self.alert("URL", message: $0.absoluteString) }
//
//            //Custom types
//
//            label.customColor[customType] = UIColor.purple
//            label.customSelectedColor[customType] = UIColor.green
//            label.customColor[customType2] = UIColor.magenta
//            label.customSelectedColor[customType2] = UIColor.green
//            label.customColor[customType3] = UIColor.red
//            label.customSelectedColor[customType3] = UIColor.red
//            label.customColor[customType4] = UIColor.yellow
//            label.customSelectedColor[customType4] = UIColor.yellow
//            label.customColor[customType5] = UIColor.green
//            label.customSelectedColor[customType5] = UIColor.green
            
//            label.configureLinkAttribute = { (type, attributes, isSelected) in
//                var atts = attributes
//                switch type {
//                case customType3:
//                    atts[NSAttributedString.Key.font] = isSelected ? UIFont.boldSystemFont(ofSize: 16) : UIFont.boldSystemFont(ofSize: 14)
//                default: ()
//                }
//                return atts
//            }

//            label.handleCustomTap(for: customType) { self.alert("Custom type", message: $0) }
//            label.handleCustomTap(for: customType2) { self.alert("Custom type", message: $0) }
//            label.handleCustomTap(for: customType3) { self.alert("Custom type", message: $0) }
        }
        
        label1.frame = CGRect(x: 20, y: 380, width: view.frame.width - 40, height: 160)
        view.addSubview(label1)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func alert(_ title: String, message: String) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        vc.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        present(vc, animated: true, completion: nil)
    }

}

